package com.csw.shuanfa.CodeImprove.CompleteFutureLinkedBlockingQueue.RequestMergeLinkedBlockingQueue;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;

public class RequestMerge {
    @Autowired
    CommodityService commodityService;

    @Test
    void context() throws ExecutionException, InterruptedException {
        //CountDownLatch来让主线程等待
        CountDownLatch countDownLatch = new CountDownLatch(100);
        for (int i = 0; i < 100; i++) {
            final String code = "code" + i;
            Thread thread = new Thread(() -> {
                try {
                    Map<String, Object> map = commodityService.queryCommodity("000" + code);
                    //Thread.currentThread().getName() + "的查询结果是:" + map);
                } catch (Exception e) {
                    //Thread.currentThread().getName() + "出现异常:" , e);
                    e.printStackTrace();
                }
                countDownLatch.countDown();
            });
            thread.setName("price-thread-" + code);
            thread.start();
        }
        countDownLatch.await();
    }
}
